/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.widgets;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.php.internal.ui.PHPUiPluginExt;
import org.eclipse.php.internal.ui.preferences.formatter.Node;
import org.eclipse.php.internal.ui.preferences.formatter.WhiteSpaceOptions;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;

/**
 * Encapsulates a view of the options tree which is structured by syntactical
 * element.
 *
 * @author Sean W. Quinn (swquinn@gmail.com)
 */
abstract public class SyntaxComponent implements ISyntaxComponent {

	protected final String PREF_NODE_KEY = PHPUiPluginExt.PLUGIN_ID + "formatter_page.white_space_tab_page.node"; //$NON-NLS-1$

	/**
   *
   */
	protected final List fIndexedNodeList;
	protected final List fTree;

	protected ContainerCheckedTreeViewer treeViewer;
	protected Composite composite;

	protected Node fLastSelected = null;

	public SyntaxComponent(final Map workingValues) {
		fIndexedNodeList = new ArrayList();
		fTree = new WhiteSpaceOptions().createAltTree(workingValues);
		WhiteSpaceOptions.makeIndexForNodes(fTree, fIndexedNodeList);
	}

	public void initialize() {
		treeViewer.addCheckStateListener(this);
		treeViewer.addSelectionChangedListener(this);
		treeViewer.addDoubleClickListener(this);
		treeViewer.setInput(fTree);
		restoreSelection();
		refreshState();
	}

	public void refreshState() {
		final ArrayList checked = new ArrayList(100);
		for (Iterator iter = fTree.iterator(); iter.hasNext();)
			((Node) iter.next()).getCheckedLeafs(checked);
		treeViewer.setGrayedElements(new Object[0]);
		treeViewer.setCheckedElements(checked.toArray());
	}

	abstract public void checkStateChanged(CheckStateChangedEvent event);
	abstract public void selectionChanged(final SelectionChangedEvent event);
	abstract public void restoreSelection();

	public void doubleClick(DoubleClickEvent event) {
		final ISelection selection = event.getSelection();
		if (selection instanceof IStructuredSelection) {
			final Node node = (Node) ((IStructuredSelection) selection).getFirstElement();
			treeViewer.setExpandedState(node, !treeViewer.getExpandedState(node));
		}
	}

	/**
	 *
	 */
	public Control getControl() {
		return composite;
	}
}
